package com.zhaopin.theswordreferstooffer;

import java.sql.Time;
import java.util.concurrent.TimeUnit;

/**
 * @author yin.peide
 * @date 2021-06-07 17:18
 * 面试题9： 斐波那契数列
 */
public class Offer9 {

    public static void main(String[] args) {

        int n = 30;

        long l1 = System.currentTimeMillis();
        for (int i = 0; i < 100; i++) {
            getIntFib(n);
        }
        long l2 = System.currentTimeMillis();
        System.out.println("循环等待时间： "+(l2-l1));

        //递归等待时间
        l1 = System.currentTimeMillis();
        for (int i = 0; i < 100; i++) {
            fib(n);
        }
        l2 = System.currentTimeMillis();
        System.out.println("递归等待时间： "+(l2-l1));


    }

    public static int getIntFib(int n) {
        int[] attr = new int[n + 1];
        attr[0] = 0;
        attr[1] = 1;

        for (int i = 2; i <= n; i++) {
            attr[i] = attr[i - 1] + attr[i - 2];
        }


        return attr[n];
    }

    public static int fib(int n ){
        if(n == 0){
            return 0;
        }
        if(n == 1){
            return 1;
        }
        return fib(n-1) + fib(n-2);
    }
}
